﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LeadExpress.Models;
using LeadExpress.Models.ViewPages;
using LeadExpress.Utility;

namespace LeadExpress.Repository
{
    public class HomeManagementRepository
    {
        LeadExpressEntities entities = new LeadExpressEntities();

        public HomeManagementModel GetModel(NavigationConfig cate, NavigationConfig state)
        {
            HomeManagementModel model = new HomeManagementModel();

            // Set value
            model.CateMenu = GetCategoryMenu(cate);
            model.StateMenu = GetStateMenu(state);
            model.GroupProduct = GetGroupProduct("/Upload/Product", 10, 5);

            return model;
        }


        #region Build Menu
        private List<NavigationItem> GetCategoryMenu(NavigationConfig config)
        {
            List<NavigationItem> list = new List<NavigationItem>();

            // Check conditions
            if (config != null && config.NavigationItems != null && config.NavigationItems.Count > 0)
            {
                foreach (var item in config.NavigationItems)
                {
                    NavigationItem link = new NavigationItem();
                    link.ItemLink = item.ItemLink;
                    link.ItemName = item.ItemName;

                    list.Add(link);
                }
            }

            return list;
        }
        private List<NavigationItem> GetStateMenu(NavigationConfig config)
        {
            List<NavigationItem> list = new List<NavigationItem>();

            // Check conditions
            if (config != null && config.NavigationItems != null && config.NavigationItems.Count > 0)
            {
                foreach (var item in config.NavigationItems.Where(c=>c.IsTop == true))
                {
                    NavigationItem link = new NavigationItem();
                    link.ItemLink = item.ItemLink;
                    link.ItemName = item.ItemName;

                    list.Add(link);
                }
                foreach (var item in config.NavigationItems.Where(c => c.IsTop == false).OrderBy(c => c.ItemName))
                {
                    NavigationItem link = new NavigationItem();
                    link.ItemLink = item.ItemLink;
                    link.ItemName = item.ItemName;

                    list.Add(link);
                }
            }

            return list;
        }
        #endregion

        #region Build RV24
        private List<GroupProductModel> GetGroupProduct(string productImageUrl, int takeGroup, int take)
        {
            List<GroupProductModel> list = new List<GroupProductModel>();
            CategoryRepository cmRepository=new CategoryRepository();
            ProductManagementRepository pmRepository=new ProductManagementRepository();

            // Get all group
            var group = cmRepository.GetAll(true);
            if (group != null)
            {
                foreach (var item in group.ToList().Skip(0).Take(takeGroup))
                {
                    GroupProductModel model = new GroupProductModel();

                    model.GroupName = item.Name;
                    model.GroupLink = string.Format("/raovat/c{0}/{1}", item.ID, StringClearFormat.ClearCharacterSpecial(item.Name));
                    //model.GroupImageUrl=string.Format("/upload/icons/{1}", item.im

                    // Get product by group
                    var products = (from c in entities.Products
                                    where (c.CategoryID == item.ID || c.ShowAllCategories == true)
                                        && c.Active == true
                                        && c.Approved == true
                                        && c.StartDate <= DateTime.Now && c.EndDate >= DateTime.Now
                                    orderby c.ID descending
                                    select new
                                    {
                                        ID = c.ID,
                                        Title = c.Title,
                                        ImageUrl = c.ImageURL
                                    }).AsEnumerable().Skip(0).Take(5);

                    if (products != null && products.Count() > 0)
                    {
                        // Add list
                        List<Models.RvModel> listProduct = new List<Models.RvModel>();
                        foreach (var p in products)
                        {
                            Models.RvModel pmodel = new Models.RvModel();

                            pmodel.Title = p.Title;
                            pmodel.LinkUrl = string.Format("/raovat/d{0}/{1}", p.ID, StringClearFormat.ClearCharacterSpecial(p.Title));
                            if (!string.IsNullOrEmpty(p.ImageUrl))
                                pmodel.ImageUrl = string.Format("{0}/{1}", productImageUrl, p.ImageUrl);
                            else
                                pmodel.ImageUrl = "/images/noimage.jpg";

                            listProduct.Add(pmodel);
                        }

                        model.ProductList = listProduct;
                        list.Add(model);
                    }
                }
            }

            return list;
        }
        #endregion
    }
}